import cTitle from 'components/title';


export default {
  data() {
    return {
      refundMoney: '',
      address: [],
      checked: {},
      options: [],
      express: '',
      expressCode: '',
      expressSN: '',
      popupVisible: false,
      // 订单id
      orderId: '',
      // 租赁订单id
      id: '',

      // 订单状态
      order_status: 0,

      // 审核状态
      check_status: ''
    };
  },

  activated() {
    if(this.$route.name == 'yz_lease_apply_adopt'){
      this.order_status = 2;
      this.getYz_lease_Adress();
      this.getExpress();
    }else{
      this.applyData();
    }
    //  this.getAddress()
    //  this.getExpress()

  },

  mounted() {},
  methods: {
    getYz_lease_Adress(){
      $http.get('plugin.yz-supply-lease.api.return.return-address', {order_id : this.$route.params.id}).then(response => {
        if (response.result == 1) {
          console.log(response);
          this.address = response.data.return_address;
          this.address.forEach(item => {
            if (item.is_default == 1) {
              this.checked = item;
            }
          });
        }
      }).catch(error => {
        console.log(error);
      });
    },
    // 申请退还
    applyData() {
      $http.get('plugin.lease-toy.api.retreat.return.index', {order_id: this.$route.params.id}, '申请中...').then(response => {
        console.log(response.data, '数据');
        if (response.result == 1) {
          let objData = response.data.lease_order;
          this.refundMoney = objData.deposit_total;
          this.orderId = objData.order_id;
          this.id = objData.id;
          this.order_status = objData.return_status;
          this.check_status = objData.return_name;
          // 地址操作
          if (this.fun.isTextEmpty(response.data.return_address)) {
            this.$dialog.alert({message:'请填写退还地址'});
            this.$router.go(-1);
          }else {
            this.address = response.data.return_address;
            this.address.forEach(item => {
              if (item.is_default == 1) {
                console.log(item);
                this.checked = item;
              }
            });
          }
          // 快递操作
          this.options = response.data.express_companies;
        } else {
          this.$dialog.alert({message:response.msg});
        }
      }).catch(error => {
        console.log(error);
      });
    },

    // 获取归还地址
    getAddress() {
      $http.get('plugin.lease-toy.api.return-address.index', {}).then(response => {
        if (response.result == 1) {
          console.log(response);
        }
      }).catch(error => {
        console.log(error);
      });
    },

    chooseAddress() {
      console.log(this.checked);
    },

    expressShow() {
      this.popupVisible = true;
    },

    expressChoose(val) {
      this.initExpress();
      this.express = val.name;
      this.expressCode = val.value;
      this.popupVisible = false;
    },

    initExpress() {
      this.express = '';
      this.expressCode = '';
    },

    // 获取快递
    getExpress() {
      let url = '';
      if(this.$route.name == 'yz_lease_apply_adopt'){
        url = 'plugin.yz-supply-lease.api.return.express';
      }
      $http.get(url, {}).then(response => {
        console.log(response);
        if (response.result == 1) {
          this.options = response.data;
        }
      }).catch(error => {
        console.log(error);
      });
    },

    // 提交审核
    submit() {
      if (this.fun.isTextEmpty(this.checked.address)) {
        this.$dialog.alert({message:'请选择归还地址'});
        return;
      }

      if (this.fun.isTextEmpty(this.express)) {
        this.$dialog.alert({message:'请选择退还快递'});
        
        return;
      }

      if (this.fun.isTextEmpty(this.expressCode)) {
        this.$dialog.alert({message:'快递code不能为空'});
        
        return;
      }

      if (this.fun.isTextEmpty(this.expressSN)) {
        this.$dialog.alert({message:'快递编号不能为空'});
        
        return;
      }

      let json = {};
      let url = '';
      if(this.$route.name == 'yz_lease_apply_adopt'){
        // 中台租赁退还填写归还信息
        url = 'plugin.yz-supply-lease.api.return.return-form';
        json = {
          order_id: this.$route.params.id,
          express_no: this.expressSN,
          company_name: this.express,
          company_code: this.expressCode,
          address_id: this.checked.id,
          address:this.checked.address,
          username: this.checked.contact_name,
          tel: this.checked.mobile,
        }
      }else{
        url = 'plugin.lease-toy.api.retreat.return.submit-return';
        json = {
          order_id: this.orderId,
          lease_id: this.id,
          express_code: this.expressCode,
          express_company_name: this.express,
          express_sn: this.expressSN,
          address: this.checked.address,
          mobile: this.checked.mobile,
          realname: this.checked.contact_name
        }
      }
      $http.get(url, json, '提交中...').then(response => {
        console.log(response);
        if (response.result == 1) {
          this.$dialog.alert({message:'提交成功'});
          this.$router.go(-1);
        }else {
          this.$dialog.alert({message:'提交失败'});
          
          this.$router.go(-1);
        }
      }).catch(error => {
        console.log(error);
      });
    }
  },
  components: {cTitle}
};
